package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.pb.StrongholdPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_ELIMINATION_DEFEND_PATRONS, desc = "淘汰赛防守门客")
public class DispatchEliminateDefendPatronsCmd extends AbstractCommand {
    private List<Integer> patronsIdList;
    private List<StrongholdEliminateDefendPatrons> defendPatronsList;
    private List<UserPatrons> patronsList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdSetDefencePatronsReqMsg req = StrongholdWarProto.StrongholdSetDefencePatronsReqMsg.parseFrom(packet.getBytes());
        patronsIdList = req.getPatronsIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            return;
        }
        int activityId = activityInfo.getActivityId();
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_STRONGHOLD_NO_UNION;
            return;
        }
        //是否有资格参赛
        if (!StrongholdWarMgr.hasQualification(activityId, unionUid)) {
            ret = GameErrorCode.E_STRONGHOLD_NO_QUALIFICATION;
            return;
        }
        //是否是派遣时间
        if (!StrongholdActivityMgr.isDispatchEliminatePatronsTime()) {
            ret = GameErrorCode.E_STRONGHOLD_DEFEND_PATRONS_CAN_NOT_CHANGE;
            return;
        }
        //是否有门客
        TreeSet<Integer> set = new TreeSet<>(patronsIdList);
        for (int patronsId : set) {
            if (patronsId != 0) {
                UserPatrons patrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
                if (patrons == null) {
                    ret = GameErrorCode.E_PATRONS_NO_FOUND;
                    return;
                }
                patronsList.add(patrons.cloneUserPatrons());
            }
        }

     //  defendPatronsList = StrongholdWarMgr.dispatchDefendPatrons(activityId, unionUid, player.getUserId(), this.patronsList, player.getModule(PatronsModule.class).getUserPatronsMap());
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.StrongholdSetDefencePatronsRespMsg.Builder resp = StrongholdWarProto.StrongholdSetDefencePatronsRespMsg.newBuilder();
        resp.setRet(ret);
        if (defendPatronsList != null) {
            for (StrongholdEliminateDefendPatrons defendPatrons : defendPatronsList) {
                if (defendPatrons.getPatrons() != null) {
                    StrongholdWarProto.UnionMemberDispatchPatronsTempMsg.Builder tempMsg = StrongholdPb.parseUnionMemberDispatchPatronsTempMsg(defendPatrons);
                    resp.addPatrons(tempMsg);
                }
            }
            player.getModule(StrongholdModule.class).syncUserData();
        }
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_DEFEND_PATRONS, resp);
    }
}
